package org.crawutils.pickcommon.xmlvo;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.crawutils.pickcommon.Handle;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

public class ReadTemplate {

	private String configXml;
	private List<HtmlModel> htmlModels = new ArrayList<HtmlModel>();
	private Map<String, String> beanMap = new HashMap<String, String>();
    private Map<String,String> convertMap=new HashMap<String, String>();
	public ReadTemplate(String configXml) {
		this.configXml = this.getClass().getClassLoader()
				.getResource(configXml).getPath();
	}

	public void parseXml() {
        SAXReader reader = new SAXReader();
		Document document;
		try {
			document = reader.read(new File(configXml));
			Element rootElement = document.getRootElement();

			List<Element> beans = rootElement.elements("beans");
			if(beans!=null && beans.size()>0){
				List<Element> beansElements = beans.get(0).elements("bean");
				for (Element ele : beansElements) {
					beanMap.put(ele.attributeValue("id"), ele
							.attributeValue("class"));
				}	
			}
			

		   List<Element> converts = rootElement.elements("converts");
		   if(converts!=null && converts.size()>0){
			   List<Element> convertsElements = converts.get(0).elements("convert");
				for (Element ele : convertsElements) {
					convertMap.put(ele.attributeValue("name"), ele
							.attributeValue("class"));
				}   
		   }
		   
			
			List<Element> bodyElements = rootElement.elements("body");
			List<Element> htmlmodelElements = bodyElements.get(0).elements("htmlmodel");
			
			for (Element ele : htmlmodelElements) {
				HtmlModel model = new HtmlModel();
				model=buildCommon(ele,model);
				htmlModels.add(model);
			}
			

			
		} catch (DocumentException e1) {
			e1.printStackTrace();
		} catch (Exception ex) {
			ex.printStackTrace();
		}

	}
	
	
    
	/**
	 * 包装公共的配置
	 * @param ele
	 * @param hm
	 * @return
	 * @throws InstantiationException
	 * @throws IllegalAccessException
	 * @throws ClassNotFoundException
	 */
	private HtmlModel buildCommon(Element ele,HtmlModel hm) throws InstantiationException, IllegalAccessException, ClassNotFoundException{
		hm.setId(ele.attributeValue("id"));
		hm.setModelclass(ele.attributeValue("modelclass"));
	    hm.setQuerys(ele.attributeValue("querys"));
		List<Element> modelchildren=ele.elements("modelchild");
		for(Element childent:modelchildren){
			Modelchild modelchild=new Modelchild();
			modelchild.setQuerys(childent.attributeValue("querys"));
			modelchild.setText(childent.getTextTrim());
			String handleClass=childent.attributeValue("handle");
			if(StringUtils.isNotBlank(handleClass)){
				modelchild.setHandle((Handle)Class.forName(handleClass).newInstance());	
			}
			String convert=childent.attributeValue("convert");
			if(StringUtils.isNotBlank(convert)){
				String convertClass=convertMap.get(convert);
				modelchild.setConvert((Convert)Class.forName(convertClass).newInstance());	
			}
			modelchild.setExtract(childent.attributeValue("extract"));
			
			hm.getChildren().add(modelchild);
		}
		return hm;
	}
	public String getConfigXml() {
		return configXml;
	}

	public void setConfigXml(String configXml) {
		this.configXml = configXml;
	}


	public Map<String, String> getBeanMap() {
		return beanMap;
	}

	public void setBeanMap(Map<String, String> beanMap) {
		this.beanMap = beanMap;
	}

	
	public List<HtmlModel> getHtmlModels() {
		return htmlModels;
	}

	public void setHtmlModels(List<HtmlModel> htmlModels) {
		this.htmlModels = htmlModels;
	}

	

}
